package cn.zcf.system.controller;

import cn.zcf.basic.utils.JsonResult;
import cn.zcf.basic.utils.PageList;
import cn.zcf.system.annotation.PreAuthorize;
import cn.zcf.system.domain.Role;
import cn.zcf.system.query.RoleQuery;
import cn.zcf.system.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
/*    @Autowired
    private IRoleService service;
    @GetMapping
    @ApiOperation(value = "查询全部" )

    public List<Role> loadAll(){
        return service.loadAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Role loadById(@PathVariable("id") Long id){
        return service.loadById(id);
    }


    @PostMapping
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(name = "角色列表",sn = "role:list") //value后面用来做权限校验
    public PageList<Role> pageQuery(@RequestBody RoleQuery query){
        try {
            return service.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Role>();
        }

    }*/
    @Autowired
    public IRoleService roleService;

    /**
     * 添加或修改
     * @param role  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Role role){
        try {
            if( role.getId()!=null)
                roleService.update(role);
            else
                roleService.save(role);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            roleService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     *//*
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            roleService.(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }*/


    /**
     * 查询单个信息
     * @param id
     */
    @GetMapping("/{id}")
    public Role findOne(@PathVariable("id")Long id) {
        return roleService.loadById(id);
    }


    /**
     * 查询所有信息
     * @return
     */
    @GetMapping
    public List<Role> findAll(){
        return roleService.loadAll();
    }


    /**
     * 分页查询或高级查询
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<Role> queryPage(@RequestBody RoleQuery query) {
        return roleService.pageQuery(query);
    }

}
